<?php
/**
	Sponsor Manager (class.categories.php file)
    Copyright (C) <Tue Feb 12 17:06:31 CET 2008>  Marcos Ramírez <marcosramirez@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    
    Purpose:
    Last Modified:
**/

class Categories {
	
	// Category tree variables
	
	private static $tree_start='0';
	private static $tree_father_open_tag='<ul>';
	private static $tree_father_close_tag='</ul>';
	private static $tree_children_open_tag='<li>';
	private static $tree_children_close_tag='</li>';
	private static $tree_generated=''; // the generated tree html string.
	
	private static $categoriesNavigator;
	
	public function __construct() {
		
	}
	
	public function get_category_tree($id='1') {
		global $db;
		//query differenciation
		// if start id is 0, whe only get the categories with father=0
		if ($id=='1')
		$sql="SELECT categoryid,father,name FROM Sys_Categories 
							WHERE categoryid>='$id'
							AND father='0'
							ORDER BY father ASC";
		else
		// id start, is greater than root, we got all
		$sql="SELECT categoryid,father,name FROM Sys_Categories 
							WHERE categoryid='$id' 
							AND father!='0'
							ORDER BY father ASC";
		
		/*
		$sql="SELECT categoryid,father,name FROM Sys_Categories 
			  WHERE categoryid>='$id' 
			  and father != 0 ORDER BY father ASC";
		*/
		//echo "<p>aplicando: $sql</p>";
		$result=$db->query($sql);
		while ($Res=$db->fetch($result)) {
			echo "<p>Leyendo:".$Res['name'].'['.$Res['categoryid'].']'.'</p>';
				
			if ($this->has_children($Res['categoryid'])) {
					$resultc=$db->query("SELECT categoryid,father,name FROM Sys_Categories 
										WHERE father='{$Res['categoryid']}'");
					while ($Resc=$db->fetch($resultc)) {
						echo $Res['name'].' es padre de: '.$Resc['name'].'<br /';
						//$this->tree_generated.='<ul><li>'.$Res['name'].''.$this->get_category_tree($Resc['categoryid']).'</li></ul>';
						$this->tree_generated.='<ul><li>'.$Res['name'].''.$this->get_children($Resc['categoryid'], $Resc['name']).'</li></ul>';
					}
			} else {
				echo $Res['name'].' no tiene hijos.';
				$this->tree_generated.='<ul><li>'.$Res['name'].'</li></ul>';
				/*
				//$this->tree_generated.=$this->tree_father_open_tag.
									$this->tree_children_open_tag.
									$Res['name'].
									$this->tree_children_close_tag.
									$this->tree_father_close_tag;
			*/
			}
			
		}		
		return $this->tree_generated;
	}
	
	private function get_children ($id, $name) {
		global $db;
		unset($ret); // clean up.
		//$ret="(hijos de $name [$id]: ";
		$result2=$db->query("SELECT categoryid,father,name FROM Sys_Categories WHERE father='$id'");
		while ($Res2=$db->fetch($result2)) {
			$ret.=$this->tree_children_open_tag.$Res2['name'].$this->tree_children_close_tag;
			//return $Res2['name'];
		}
			
		return $this->tree_father_open_tag.$ret.$this->tree_father_open_tag;
	}

	
	private function get_children2 ($id, $name) {
		global $db;
		unset($ret); // clean up.
		//$ret="(hijos de $name [$id]: ";
		$result2=$db->query("SELECT categoryid,father,name FROM Sys_Categories WHERE father='$id'");
		while ($Res2=$db->fetch($result2)) {
			/*$ret.=$this->tree_children_open_tag.$Res['categoryid'].$Res['name'].$this->tree_children_close_tag;*/
			return $Res2['name'];
		}
			
		return $ret.')';
	}
	
	private function has_children ($id) {
		global $db;
		$result1=$db->query("SELECT categoryid FROM Sys_Categories WHERE father='$id'");
		if (is_array($Res=$db->fetch($result1)))
			return true;
		else 
			return false;
	}
	
	
	public  function add($category, $father=0) {
		global $db;
		//Check for allready existing categories
		$db->query("SELECT name,father FROM Sys_Categories WHERE name='$category' AND father='$father'");
		if (is_array($Res=$db->fetch()))
			return _('Error Category allready exists');
		
		//add new category
		$db->query("INSERT INTO Sys_Categories (name, father) 
					VALUES
					('$category', '$father')");
		if ($db->error()!='')
				return _('Error inserting user in dabase: ').$db->error();
			else 
				return 0;
	}
	
	public function getList() {
		global $db;
		$db->query("SELECT categoryid,name FROM Sys_Categories");
		$Res=$db->fetch();
		if (!is_array($Res)) //Return only de "root" category x)
			return array("categoryid" => 0, 
						"name" => "root");
		else 
			return $Res;
	}

	public function getListOption() {
		global $db;
		$db->query("SELECT categoryid,name FROM Sys_Categories NO_CACHE");
		while ($Res=$db->fetch()) {
			if (!is_array($Res)) //Return only de "root" category x)
				return '<option value="0">Root</option>';
	
			$return.='<option value="'.$Res['categoryid'].'"> '.$Res['name'].'</option>';
		}
		
	return '<option value="0">Root</option>'.$return;
	}

	public function getCategoriesNavigator ($id=0) {
		global $db;
		$result=$db->query("SELECT * FROM Sys_Categories ORDER BY categoryID ASC");
		while ($Res=$db->fetch()) {
			if ($this->CategoryHasChilds($Res['categoryid'])) {
				$this->categoriesNavigator.='<ul> +'.$Res['name'].'</ul>';
				$this->categoriesNavigator.=$this->getCategoriesNavigator($Res['categoryid']);
			}
		}
	}
	
	public function CategoryHasChilds ($id) {
		global $db;
		$result=$db->query("SELECT * FROM Sys_Categories WHERE father='$id'");
		if (is_array($Res=$db->fetch()))
			return true;
		else 
			return false;
	}
}
?>